<?php
namespace app\wechat\controller;

use app\wechat\controller\User;

use app\wechat\model\department; 
use app\wechat\model\userd;
use app\wechat\model\tag;
use app\wechat\model\taguser;
use app\wechat\model\userlist;

use think\db;

class IndexController
{
    
    public function login(){
        $user = new User();
        $list = $user->login('index/index');
    }
    
    public function tagUser(){
        $user = new User();
        $list = $user->getAllTag();
        $arr = [];
        foreach($list as $v){
           $arr[] = $v['tagid']; 
        }
        $arr = $user->batchTagUserList($arr);
        print_r("<pre>");
        print_r($arr);
        

        ini_set('max_execution_time', 0);
        $datas = [];
        $i = 0;
        foreach($arr as $k=>$v){
            foreach($v as $value){
                $datas[$i]['tagid'] = $k?:'';
                $datas[$i]['userid'] = $value['userid']?:'';
                $i++;
            }
        }
        print_r($datas);
        
        $taguser = new taguser();
        //$taguser->saveAll($datas);
        
    }
    
    public function tag(){
        $user = new User();
        $arr = $user->getAllTag();
        print_r("<pre>");
        print_r($arr);
        

        ini_set('max_execution_time', 0);
        $datas = [];
        $i = 0;
        foreach($arr as $k=>$v){
            $datas[$i]['tagid'] = $v['tagid']?:'';
            $datas[$i]['tagname'] = $v['tagname']?:'';
            $i++;
        }
        print_r($datas);
        $tag = new tag();
        //$tag->saveAll($datas);

    }
    
    public function user(){
        $content = '区纪委监委、区委办公室、区委组织部、区委宣传部、区委统战部、区委政法委、区编委办公室、区直属机关党委、区信访局、区府办公室、区财政局、区招商办、琶洲管委会、区机关事务局、区人大、区政协、区总工会、团区委、区妇联、区侨联';
        $content = explode('、',$content);
        //print_r($content);
        $ids = [];
        foreach($content as $v){
            $ids[] = Db::name('wechat_department')->where('name','like','%'.$v.'%')->value('id');
        }
        //print_r($ids);
        $user = new User();
        print_r("<pre>");
        //print_r($user->batchUserList($ids));
        $user = $user->batchUserList($ids);
        print_r($user);
        /*
        ini_set('max_execution_time', 0);
        $datas = [];
        $i = 0;
        foreach($user as $k=>$v){
            $datas[$i]['user_login'] = $v['name']?:'';
            $datas[$i]['mobile'] = $v['mobile']?:'';
            $datas[$i]['openid'] = $v['userid']?:'';
            $i++;
        }
        $userlist = new userlist();
        //$userlist->saveAll($datas);
        */
        
        
        //部门 会员关联
        
        ini_set('max_execution_time', 0);
        $datas = [];
        $i = 0;
        foreach($user as $k=>$v){
            foreach($v['department'] as $value){
                $datas[$i]['user_id'] = $v['userid'];
                $datas[$i]['department_id'] = $value;
                $i++;
            }
        }
        print_r($datas);
        $userd = new Userd();
        //$userd->saveAll($datas);
        //cmf_wechat_department_user
        
    }
    
    public function department(){
        
        $user = new User();
        $department = new department();
        $list = $user->getDepartment();
        
        ini_set('max_execution_time', 0);
        $datas = [];
        $i = 0;
        foreach($list as $k=>$v){
            $datas[$i]['id'] = $v['id'];
            $datas[$i]['name'] = $v['name'];
            $datas[$i]['parentid'] = $v['parentid'];
            $datas[$i]['order'] = $v['order'];
            $i++;
        }
        //$department->saveAll($datas);

        
        //file_put_contents('department.json',json_encode($this->getAllUserList()));
        print_r($list);
    }

    
    
}
